package com.axzhengxin.platform.business.service.impl;

import java.util.List;

import com.axzhengxin.platform.business.domain.BizApplyInfo;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.axzhengxin.platform.business.mapper.BizFactoringInfoMapper;
import com.axzhengxin.platform.business.domain.BizFactoringInfo;
import com.axzhengxin.platform.business.service.IBizFactoringInfoService;

/**
 * 保理信息Service业务层处理
 *
 * @author axzhengxin
 * @date 2021-04-01
 */
@Service
public class BizFactoringInfoServiceImpl implements IBizFactoringInfoService {
    @Autowired
    private BizFactoringInfoMapper bizFactoringInfoMapper;

    /**
     * 查询保理信息
     *
     * @param factoringId 保理信息ID
     * @return 保理信息
     */
    @Override
    public BizFactoringInfo selectBizFactoringInfoById(String factoringId) {
        return bizFactoringInfoMapper.selectBizFactoringInfoById(factoringId);
    }

    /**
     * 查询保理信息列表
     *
     * @param bizFactoringInfo 保理信息
     * @return 保理信息
     */
    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<BizFactoringInfo> selectBizFactoringInfoList(BizFactoringInfo bizFactoringInfo) {
        List<BizFactoringInfo> bizFactoringInfos = bizFactoringInfoMapper.selectBizFactoringInfoList(bizFactoringInfo);
        bizFactoringInfos = buildBizApplyList(bizFactoringInfos);
        return bizFactoringInfos;
    }

    /**
     * 新增保理信息
     *
     * @param bizFactoringInfo 保理信息
     * @return 结果
     */
    @Override
    public int insertBizFactoringInfo(BizFactoringInfo bizFactoringInfo) {
        bizFactoringInfo.setCreateTime(DateUtils.getNowDate());
        return bizFactoringInfoMapper.insertBizFactoringInfo(bizFactoringInfo);
    }

    /**
     * 修改保理信息
     *
     * @param bizFactoringInfo 保理信息
     * @return 结果
     */
    @Override
    public int updateBizFactoringInfo(BizFactoringInfo bizFactoringInfo) {
        bizFactoringInfo.setUpdateTime(DateUtils.getNowDate());
        return bizFactoringInfoMapper.updateBizFactoringInfo(bizFactoringInfo);
    }

    /**
     * 批量删除保理信息
     *
     * @param factoringIds 需要删除的保理信息ID
     * @return 结果
     */
    @Override
    public int deleteBizFactoringInfoByIds(String[] factoringIds) {
        return bizFactoringInfoMapper.deleteBizFactoringInfoByIds(factoringIds);
    }

    /**
     * 删除保理信息信息
     *
     * @param factoringId 保理信息ID
     * @return 结果
     */
    @Override
    public int deleteBizFactoringInfoById(String factoringId) {
        return bizFactoringInfoMapper.deleteBizFactoringInfoById(factoringId);
    }


    public List<BizFactoringInfo> buildBizApplyList(List<BizFactoringInfo> bizFactoringInfos) {
        bizFactoringInfos.stream().
                forEach(bizFactoringInfo -> {
                    String pciBelongNo = bizFactoringInfo.getPciBelongNo();
                    if (StringUtils.isNotBlank(pciBelongNo)) {
                        String biz_industry_type = DictUtils.getDictLabel("biz_industry_type", pciBelongNo);
                        if (StringUtils.isNotBlank(biz_industry_type)) {

                            bizFactoringInfo.setPciBelongType(biz_industry_type);
                        }
                    }
                    String customerAreaNo = bizFactoringInfo.getCustomerAreaNo();
                    if (StringUtils.isNotBlank(customerAreaNo)) {
                        String area_codes = DictUtils.getDictLabel("area_codes", customerAreaNo);
                        if (StringUtils.isNotBlank(area_codes)) {
                            bizFactoringInfo.setCustomerArea(area_codes);

                        }
                    }
                });
        return bizFactoringInfos;
    }
}